<script setup>
import * as API from '@/api/item/voucher.js'
import { getLocalTime } from "@/utils/time.js"
import { ElMessage } from 'element-plus'
import { ref, reactive, onMounted } from 'vue'
import * as api from "@/api/item/voucher.js";


/* ----- search ----- */


const searchOption = reactive({
  voucherName: null,
})

// 用于表格
const pn = ref(1)
const ps = ref(10)
const total = ref(0)
const couponTypeList = ref([])
const couponType = ref([
  {label: "代金券", value: 0},
  {label: "赠品卷", value: 1},
])

const dialogFormVisible = ref(false)
const couponParams = reactive({
  id: null,
  name: null,
  type: null,
  description: null,
  validStartDate: null,
  validEndDate: null,
})

const getPage = async (pager = 1)=>{
  pn.value = pager
  const result = await API.reqPageVoucher(
      pn.value,
      ps.value,
      searchOption.voucherName,
  )

  if (result.code === "0") {
    total.value = result.data.total
    couponTypeList.value = result.data.items
  }else {
    ElMessage({type:"error", message:"获取数据失败！"})
  }
}

const addCoupon = ()=>{
  dialogFormVisible.value = true
  Object.assign(couponParams,{
    id: null,
    name: null,
    type: null,
    description: null,
    validStartDate: null,
    validEndDate: null,
  })
}

const updateCoupon = (row)=>{
  dialogFormVisible.value = true
  Object.assign(couponParams,row)
}

const deleteCoupon = async (id)=>{
  const result = await API.reqDeleteVoucher(id)
  if (result.code === '0') {
    ElMessage({ type: 'success', message: '删除成功' })
    await getPage(couponTypeList.value.length > 1 ? pn.value : pn.value - 1);
  }
}

const disableCoupon = async (id, flag)=>{
  const result = await API.reqBanVoucher(id, flag)
  if (result != null) {
    ElMessage({
      message: flag === 0 ? '启用成功' : '禁用成功',
      type: 'success',
    })
    await getPage()
  }
}

const search = async ()=>{
  await getPage()
}

const cancel = ()=>{
  dialogFormVisible.value = false
}

const confirm = async ()=>{
  let result
  if(couponParams.id){
    result = await API.reqUpdateVoucher(couponParams)
  }else{
    result = await API.reqSaveVoucher(couponParams)
  }
  if (result.code === '0') {
    ElMessage({ type: 'success', message: couponParams.id? '修改成功':'添加成功',})
    dialogFormVisible.value = false
    await getPage()
  } else {
    dialogFormVisible.value = false
    //提示消息
    ElMessage({ type: 'error', message: couponParams.id ? '修改失败' : '添加失败' });
  }
}

onMounted(()=>{
  getPage()
})

</script>
<script>
export default {
  name: "coupon_type",
}
</script>


<template>
  <!-- ----------------------------------- search ----------------------------------- -->
  <el-card>
    <el-scrollbar always>
      <el-form class="search-card">
        <div class="search-input">
          <el-form-item label="卡券名">
            <el-input placeholder="请输入卡券名" v-model="searchOption.voucherName" clearable></el-input>
          </el-form-item>
        </div>
        <el-form-item>
          <el-button type="primary" size="default" @click="search" >搜索</el-button>
        </el-form-item>
      </el-form>
    </el-scrollbar>
  </el-card>
  <!-- ----------------------------------- search ----------------------------------- -->

  <!-- ----------------------------------- table ----------------------------------- -->
  <el-card>
    <el-button type="primary" size="default" @click="addCoupon">添加卡券</el-button>
    <el-table style="margin: 10px 0;" border :data="couponTypeList">
      <el-table-column label="#" align="center" type="index"></el-table-column>
      <el-table-column label="卡券名" align="center" prop="name" show-overflow-tooltip></el-table-column>
      <el-table-column label="卡券类型" align="center" prop="type" show-overflow-tooltip>
        <template #="{row}">
          <span>{{ couponType[row.type].label }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" show-overflow-tooltip></el-table-column>
      <el-table-column label="有效期起始时间" align="center" prop="validStartDate" show-overflow-tooltip>
        <template #="{row}">
          <span>{{ getLocalTime(row.validStartDate, 1)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期结束时间" align="center" prop="validEndDate" show-overflow-tooltip>
        <template #="{row}">
          <span>{{ getLocalTime(row.validEndDate, 1)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" show-overflow-tooltip>
        <template #="{row}">
          <span>{{ getLocalTime(row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" show-overflow-tooltip>
        <template #="{row}">
          <span>{{ getLocalTime(row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="banFlag" show-overflow-tooltip>
        <template #="{ row, $index}">
          <p v-if="row.banFlag === 0"></p>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200px" align="center">
        <template #="{ row, $index }">
          <el-row>
            <el-col :span="12">
              <el-button type="primary" size="small" icon="Edit" @click="updateCoupon(row)">编辑</el-button>
            </el-col>
            <el-col :span="12">
              <el-popconfirm :title="`确定要删除?${row.name}`" width="260px" @confirm="deleteCoupon(row.id)">
                <template #reference>
                  <el-button type="primary" size="small" icon="Delete">删除</el-button>
                </template>
              </el-popconfirm>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-button type="warning" size="small" icon="Edit" @click="disableCoupon(row.id, 1)">禁用</el-button>
            </el-col>
            <el-col :span="12">
              <el-button type="success" size="small" icon="Edit" @click="disableCoupon(row.id, 0)">启用</el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="getPage"
        @current-change="getPage"
        v-model:current-page="pn"
        v-model:page-size="ps"
        :page-sizes="[10, 20, 50, 100]"
        :background="true"
        layout="prev, pager, next, jumper, ->, sizes, total"
        :total="total"
    />
  </el-card>
  <!-- ----------------------------------- table ----------------------------------- -->

  <!-- ----------------------------------- dialog ----------------------------------- -->
  <el-dialog
      v-model="dialogFormVisible"
      :title="couponParams.id ? '添加卡券' : '修改卡券'"
  >
    <el-form style="width: 80%" :model="couponParams">
      <el-form-item label="卡券名" label-width="120px">
        <el-input v-model="couponParams.name"></el-input>
      </el-form-item>
      <el-form-item label="卡券类型" label-width="120px">
        <el-select v-model="couponParams.type" clearable placeholder="卡券类型" style="width:180px">
          <el-option
              v-for="item in couponType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="卡券描述" label-width="120px">
        <el-input v-model="couponParams.description"></el-input>
      </el-form-item>
      <el-form-item label="有效期起始时间" label-width="120px">
        <el-date-picker
            v-model="couponParams.validStartDate"
            type="date"
            placeholder="有效期起始时间"
        />
      </el-form-item>
      <el-form-item label="有效期结束时间" label-width="120px">
        <el-date-picker
            v-model="couponParams.validEndDate"
            type="date"
            placeholder="有效期结束时间"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button type="primary" size="default" @click="cancel">取消</el-button>
      <el-button type="primary" size="default" @click="confirm">确定</el-button>
    </template>
  </el-dialog>
  <!-- ----------------------------------- dialog ----------------------------------- -->
</template>

<style scoped lang="css">
.search-card {
  display: flex;
  align-items: center;
  flex-direction: row;
}
.search-input {
  display: flex;
  align-items: center;
  flex: 1 0 auto;
}
.search-input > * {
  flex: 0 1 auto;
  margin-right: 2rem;
}
.search-card .el-form-item {
  margin-bottom: 0;
}
.el-card {
  margin-bottom: 2rem;
}
</style>